import { defineStore } from "pinia";
import { ref } from "vue";

export const orderStore = defineStore(
  "hy-order",
  () => {
    // 表单数据
    const orderForm = ref({
      seller_memo: "",
      card_id: "",
      products: [],
    });
    // 地址数据
    const formAddress = ref({
      receiver_address: "",
      receiver_mobile: "",
      receiver_name: "",
      id: "",
    });

    const setOrderForm = (data) => {
      orderForm.value = { ...orderForm.value, ...data };
    };

    const setAddress = (data) => {
      formAddress.value = { ...formAddress.value, ...data };
    };

    const setOrderProducts = (products) => {
      orderForm.value.products = products;
    };

    const setSellerMemo = (memo) => {
      orderForm.value.seller_memo = memo;
    };

    const clearOrderData = () => {
      orderForm.value = {
        seller_memo: "",
        card_id: "",
        products: [],
      };
      formAddress.value = {
        receiver_address: "",
        receiver_mobile: "",
        receiver_name: "",
        id: "",
      };
    };
    return {
      orderForm,
      formAddress,
      setOrderForm,
      setAddress,
      setOrderProducts,
      setSellerMemo,
      clearOrderData,
    };
  },
  {
    // 网页端配置
    // persist: true,
    // 小程序端配置
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key);
        },
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
      },
    },
  }
);
